package com.leps.nq.tx.core;

import cn.hutool.core.util.StrUtil;
import com.leps.nq.bu.entity.BuPlc;
import com.leps.nq.bu.entity.BuPump;
import com.leps.nq.bu.service.IBuPlcService;
import com.leps.nq.bu.service.IBuPumpService;
import com.leps.nq.common.util.SpringUtils;
import com.leps.nq.op.entity.OpAlarm;

import java.util.Date;

public class AlarmFactory {
    private static IBuPlcService plcService = SpringUtils.getBean(IBuPlcService.class);
    private static IBuPumpService pumpService = SpringUtils.getBean(IBuPumpService.class);

    public static OpAlarm createBasicAlarm(String alarmType, String alarmValue, Integer pumpId, int plcId, int stationId, int areaId, int custId) {
        OpAlarm alarm = new OpAlarm();
        alarm.setAlarmType(alarmType);
        alarm.setAlarmTime(new Date());
        alarm.setAlarmValue(alarmValue);
        alarm.setCreateTime(new Date());
        alarm.setCreateBy("system");
        alarm.setPumpId(pumpId);
        alarm.setPlcId(plcId);
        alarm.setStationId(stationId);
        alarm.setAreaId(areaId);
        alarm.setCustId(custId);
        alarm.setHandled(0);
        return alarm;
    }

    public static OpAlarm createPlcOfflineAlarm(int plcId, String createBy) {
        // get plc info
        BuPlc plc = plcService.getById(plcId);
        // generate a Alarm
        String alarmValue = StrUtil.format("PLC[{}]断网", plc.getPlcName());
        return createBasicAlarm("断网", alarmValue, null, plcId, plc.getStationId(), plc.getAreaId(), plc.getCustId());
    }

    public static OpAlarm createPlcPwdAlarm(int plcId) {
        // get plc info
        BuPlc plc = plcService.getById(plcId);
        // format alarm value
        String alarmValue = StrUtil.format("PLC[{}]密码错误", plc.getPlcName());
        return createBasicAlarm("故障", alarmValue, null, plcId, plc.getStationId(), plc.getAreaId(), plc.getCustId());
    }

    public static OpAlarm createLiquidLevelAlarm(int pumpId, String alarmValue) {
        return createPumpAlarm(pumpId, "液位", alarmValue);
    }

    public static OpAlarm createPressureAlarm(int pumpId, String alarmValue) {
        return createPumpAlarm(pumpId, "压力", alarmValue);
    }

    public static OpAlarm createPumpAlarm(int pumpId, String alarmType, String alarmValue) {
        BuPump pump = pumpService.getById(pumpId);
        return createBasicAlarm(alarmType, alarmValue, pumpId, pump.getPlcId(), pump.getStationId(), pump.getAreaId(), pump.getCustId());
    }

    public static String tooHighLiquidLevelAlarmValue(String pumpName, double liquidLevel) {
        return StrUtil.format("泵[{}]液位过高：[{}CM]", pumpName, liquidLevel);
    }

    public static String tooLowLiquidLevelAlarmValue(String pumpName, double liquidLevel) {
        return StrUtil.format("泵[{}]液位过低：[{}CM]", pumpName, liquidLevel);
    }

    public static String tooLowPressureAlarmValue(String pumpName, double pressure) {
        return StrUtil.format("泵[{}]压力过低：[{}Mpa]", pumpName, pressure);
    }

    public static String tooHighPressureAlarmValue(String pumpName, double pressure) {
        return StrUtil.format("泵[{}]压力过高：[{}Mpa]", pumpName, pressure);
    }


}
